﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Card.Utility
{
    public class DateTimeUtility
    {
        /// <summary>
        /// 日期转化为字符串(yyyy-MM-yy)
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime date)
        {
            if (DateTime.MaxValue.CompareTo(date) == 0
                || DateTime.MinValue.CompareTo(date) == 0)
            {
                return string.Empty;
            }
            return DateTimeToString(date, "yyyy-MM-dd");
        }

        /// <summary>
        /// 日期转化为字符串(yyyy-MM-yy HH:mm:ss)
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string DateTimeToLongString(DateTime date)
        {
            return DateTimeToString(date, "yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 日期转化为字符串
        /// </summary>
        /// <param name="date"></param>
        /// <param name="strFormater"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime date, string strFormater)
        {
            if (DateTime.MaxValue.CompareTo(date) == 0
                || DateTime.MinValue.CompareTo(date) == 0)
            {
                return string.Empty;
            }
            else
            {
                return date.ToString(strFormater);
            }
        }
        /// <summary>
        /// 计算两个日期间相差的天数 date1 - date2
        /// 如：2012.03.05 - 2012.03.02=3
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static int DateDiffByDay(DateTime date1, DateTime date2)
        {
            return new TimeSpan(date1.Ticks).Subtract(new TimeSpan(date2.Ticks)).Days;
        }
    }
}
